<?php


namespace app\controller\api;


use think\facade\Db;

class User extends Base
{
    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 货代运输 名片
     * @author: dongdong
     * @date: 2025/2/8 11:43
     */
    public function business_card()
    {
        try {
            $page = input('page') ?? 1;
            $limit = input('limit') ?? 10;
            $page = ($page - 1) * $limit;

            $keyword = input('keyword') ?? '';
            $area_id = input('area_id') ?? '';
            $service_index = input('service_index') ?? '';
            $transport_index = input('transport_index') ?? '';

            $where = "i.profile is not null and i.profile!=''";
            if ($keyword) {
                $where .= " and (u.username like '%$keyword%' or u.username_ru like '%$keyword%') ";
            }
            if ($area_id) {
                if (is_numeric($area_id)) {
                    $where .= " and i.area_new_id='$area_id' ";
                } else {
                    $city_arr = explode(',', $area_id);
                    if ($city_arr) {
                        $d_country = $city_arr[0];
                        $d_province = $city_arr[1];
                        $d_city = $city_arr[2];
                        if ($d_city != '-1') {
                            $where .= " and i.area_new_id='$d_city' ";
                        } else {
                            if ($d_province != '-1') {
                                $where .= " and i.province_id='$d_province' ";
                            } else {
                                $where .= " and i.country=$d_country";
                            }
                        }
                    }
                }
            }
            if ($service_index) {
                $where .= " and i.provide_services_id like '%$service_index%' ";
            }
            if ($transport_index) {
                $where .= " and i.provide_transport_id like '%$transport_index%' ";
            }


            $list = Db::connect('fast_mysql')->table("fa_user")->alias("u")->join("fa_user_info i", "u.id=i.user_id")
                ->where("i.identity_id", "in", "5,11,58,59,6,50,51,52")->where($where)->order("u.level desc")->limit($page, $limit)->select();

            $arr = [];
            foreach ($list as $k => &$v) {
                $v['area'] = Db::connect('fast_mysql')->table("fa_area")->where(['id' => $v['area_new_id']])->find();
                $v['order_count'] = Db::table('store_quotation')->alias("q")->join("store_delivery d", "q.delivery_id=d.id")
                    ->where(['d.status' => 4, 'q.user_id' => $v['user_id']])->count();
                $v['checked'] = $v['level'];
                $v['car'] = Db::table("store_car")->where(['user_id' => $v['user_id'], 'mode' => 2,'is_deleted'=>0])->select();
                $v['cars'] = [
                    ['id' => 2, 'count' => Db::table("store_car")->where(['user_id' => $v['user_id'], 'mode' => 2])->count()],
                    ['id' => 3, 'count' => Db::table("store_car")->where(['user_id' => $v['user_id'], 'mode' => 3])->count()],
                    ['id' => 4, 'count' => Db::table("store_car")->where(['user_id' => $v['user_id'], 'mode' => 4])->count()],
                    ['id' => 5, 'count' => Db::table("store_car")->where(['user_id' => $v['user_id'], 'mode' => 5])->count()]
                ];
               /* $com_count = Db::table("store_comment")->where(['service_user_id' => $v['user_id']])->count();
                $sum_score = Db::table("store_comment")->where(['service_user_id' => $v['user_id']])->sum('score');
                $v['rating_score'] = round(bcdiv($sum_score,$com_count,2),1);//评分*/
                $v['car_count'] = Db::table("store_car")->where(['user_id' => $v['user_id'],'is_deleted'=>0])->count();
//                if($k==0){
//                    $v['checked'] = 1;
//                }
                array_push($arr, $v);
            }
            $this->api_result(200, '请求成功', ['data' => $arr]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }
    }

    public function business_cards()
    {
        try {
            $mode = input('mode') ?? 2;
            $user_id = input('user_id') ?? 1;
            $arr = Db::table("store_car")->where(['user_id' => $user_id, 'mode' => $mode])->select();
            $this->api_result(200, '请求成功', ['data' => $arr]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }
    }


    /**
     * @notes 企业名片详情
     * @author: dongdong
     * @date: 2025/2/8 17:23
     */
    public function business_info()
    {
        try {
            $user_id = input('user_id') ?? 0;
            if ($user_id) {
                $v = Db::connect('fast_mysql')->table("fa_user")->alias("u")->join("fa_user_info i", "u.id=i.user_id")->where("user_id=$user_id")->find();
                $v['area'] = Db::connect('fast_mysql')->table("fa_area")->where(['id' => $v['area_new_id']])->find();

                $v['order_count'] = $v['user_id'];
                $v['guanzhu_count'] = Db::connect("fast_mysql")->table("fa_user_guanzhu")->where("release_type='user'")->where(['guanzhu_id' => $user_id])->count();
                $v['comment_count'] = Db::connect("fast_mysql")->table("fa_message_board")->where("user_id=$user_id")->where("evaluate!=0")->count();

                $count0 = Db::connect("fast_mysql")->table("fa_release_goods")->where("deletetime is null and user_id=$user_id")->count();
                $count1 = Db::connect("fast_mysql")->table("fa_release_transport")->where("deletetime is null and user_id=$user_id")->count();
                $count2 = Db::connect("fast_mysql")->table("fa_release_crossborder")->where("deletetime is null and user_id=$user_id")->count();
                $count3 = Db::connect("fast_mysql")->table("fa_release_professional")->where("deletetime is null and user_id=$user_id")->count();
                $count4 = Db::connect("fast_mysql")->table("fa_release_lifead")->where("deletetime is null and user_id=$user_id")->count();
                $count5 = Db::connect("fast_mysql")->table("fa_release_exposure")->where("deletetime is null and user_id=$user_id")->count();
                $count6 = Db::connect("fast_mysql")->table("fa_release_news")->where("deletetime is null and user_id=$user_id")->count();
                $v['release_count'] = $count0 + $count1 + $count2 + $count3 + $count4 + $count5 + $count6;

                $v['car_count'] = Db::table("store_car")->where(['user_id' => $user_id, 'is_deleted' => 0])->count();

                $v['is_bond'] = 0; //是否是保证金大户
                $v['is_insure'] = 1; //是否是保险大户
                $v['is_agency'] = 1; //是否是代收
                $v['receiving_order_num'] = 0; //接单量
                $v['goods_comment_rate'] = '100'; //好评率
                $v['on_time_rate'] = '100'; //准时率
                $v['accident_rate'] = '0'; //事故率
                $v['complaint_rate'] = '0'; //投诉率

                $v['identity_type'] = Db::connect('fast_mysql')->table("fa_identity")->where(['id' => $v['identity_id']])->value('type'); //身份

                $cfd_transports = explode(';', $v['cfd_transports']);
                $mdd_transports = explode(';', $v['mdd_transports']);
                $arr = [];
                foreach ($cfd_transports as $k => $vv) {
                    if ($vv) {
                        $ar = explode(',', $vv);
                        if ($ar[2] == '-1' && $ar[1] == '-1') {
                            $cfd = $this->areas()[$ar[0]];
                        } elseif ($ar[2] == '-1' && $ar[1] != '-1') {
                            $cfd = $this->citys($ar[1]);
                        } else {
                            $cfd = $this->citys($ar[2]);
                        }
                        $cfd['id'] = $vv;
                        $arr[$k]['cfd'] = $cfd;
                    }
                }
                foreach ($mdd_transports as $k => $vv) {
                    if ($vv) {
                        $ar = explode(',', $vv);
                        if ($ar[2] == '-1' && $ar[1] == '-1') {
                            $mdd = $this->areas()[$ar[0]];
                        } elseif ($ar[2] == '-1' && $ar[1] != '-1') {
                            $mdd = $this->citys($ar[1]);
                        } else {
                            $mdd = $this->citys($ar[2]);
                        }
                        $mdd['id'] = $vv;
                        $arr[$k]['mdd'] = $mdd;
                    }
                }
                $v['luxian'] = $arr;

                $this->api_result(200, '请求成功', $v);
            } else {
                $this->api_result(400, '请求失败', []);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', []);
        }
    }

    //城市
    public function city($id)
    {
        if ($id) {
            return Db::connect("fast_mysql")->table("fa_area")->field("id,shortname,shortname_ru,name,name_ru,mergename,mergename_ru,country")->where("id", "in", $id)->find();
        } else {
            return [];
        }
    }

    public function citys($id)
    {
        if ($id) {
            return Db::connect("fast_mysql")->table("fa_area")->field("shortname,shortname_ru,name,name_ru,mergename,mergename_ru,country")->where("id", "in", $id)->find();
        } else {
            return [];
        }
    }


    /**
     * 请求视频分钟积分 是否够用
     */
    public function request_minute_score()
    {
        try {
            $user_id = input('user_id') ?? 0;
            $target_id = input('target_id') ?? 0;
            $identity_id = Db::connect("fast_mysql")->table("fa_user_info")->where(["user_id" => $user_id])->find();
            $identity_tid = Db::connect("fast_mysql")->table("fa_user_info")->where(["user_id" => $target_id])->find();
            $fa_user = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $user_id])->field('score,level')->find();
            $score = $fa_user['score'];
            $level = $fa_user['level'];
            $flag = 0;
            $type = 2;
            $identity_utype = Db::connect("fast_mysql")->table("fa_identity")->where(["id" => $identity_id['identity_id']])->value('type');
            $identity_ttype = Db::connect("fast_mysql")->table("fa_identity")->where(["id" => $identity_tid['identity_id']])->value('type');
            if ($identity_id['country'] == 0 && $identity_tid['country'] == 0) {

            } else {
                if ($identity_ttype != 11 && $identity_utype != 11) {
                    $type = 1;
                }
            }
            if ($identity_id['identity_id']) {
                $identity_type = Db::connect("fast_mysql")->table("fa_identity")->where(["id" => $identity_id['identity_id']])->value('type');

                if ($identity_type == 0) { //供应商
                    if ($level == 0) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=176")->value("val");
                    } elseif ($level == 1) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=201")->value("val");
                    } elseif ($level == 2) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=226")->value("val");
                    } elseif ($level == 3) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=251")->value("val");
                    }
                    if ($score > $video_score) {
                        $flag = 1;
                    }
                } elseif ($identity_type == 2) { //货代公司
                    if ($level == 0) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=27")->value("val");
                    } elseif ($level == 1) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=65")->value("val");
                    } elseif ($level == 2) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=103")->value("val");
                    } elseif ($level == 3) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=141")->value("val");
                    }
                    if ($score > $video_score) {
                        $flag = 1;
                    }
                } elseif ($identity_type == 3) { //采购商
                    if ($level == 0) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=276")->value("val");
                    } elseif ($level == 1) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=301")->value("val");
                    } elseif ($level == 2) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=326")->value("val");
                    } elseif ($level == 3) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=351")->value("val");
                    }
                    if ($score > $video_score) {
                        $flag = 1;
                    }
                } elseif ($identity_type == 6) { //运输公司
                    if ($level == 0) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=27")->value("val");
                    } elseif ($level == 1) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=65")->value("val");
                    } elseif ($level == 2) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=103")->value("val");
                    } elseif ($level == 3) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=141")->value("val");
                    }
                    if ($score > $video_score) {
                        $flag = 1;
                    }
                } elseif ($identity_type == 9) { //服务商
                    if ($level == 0) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=176")->value("val");
                    } elseif ($level == 1) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=201")->value("val");
                    } elseif ($level == 2) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=226")->value("val");
                    } elseif ($level == 3) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=251")->value("val");
                    }
                    if ($score > $video_score) {
                        $flag = 1;
                    }
                } elseif ($identity_type == 11) { //翻译
                    if ($level == 0) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=376")->value("val");
                    } elseif ($level == 1) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=401")->value("val");
                    } elseif ($level == 2) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=426")->value("val");
                    } elseif ($level == 3) {
                        $video_score = Db::connect("fast_mysql")->table("fa_vip_set_privilege")->where("id=451")->value("val");
                    }
                    if ($score > $video_score) {
                        $flag = 1;
                    }
                } else {
                    $flag = 1;
                }
            }
            $this->api_result(200, '请求成功', ['score' => $score, 'flag' => $flag, 'video_score' => $video_score, 'type' => $type]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', []);
        }
    }

    public function areas()
    {
        $data = [
            [
                'shortname' => '中国',
                'shortname_ru' => 'Китай',
                'name' => '中国',
                'name_ru' => 'Китай',
                'mergename' => 'Китай',
                'mergename_ru' => 'Китай',
                'country' => 0
            ],
            [
                'shortname' => '俄罗斯',
                'shortname_ru' => 'Россия',
                'name' => '俄罗斯',
                'name_ru' => 'Россия',
                'mergename' => 'Россия',
                'mergename_ru' => 'Россия',
                'country' => 1
            ],
            [
                'shortname' => '哈萨克斯坦',
                'shortname_ru' => 'Казахстан',
                'name' => '哈萨克斯坦',
                'name_ru' => 'Казахстан',
                'mergename' => 'Казахстан',
                'mergename_ru' => 'Казахстан',
                'country' => 2
            ],
            [
                'shortname' => '白俄罗斯',
                'shortname_ru' => 'Беларусь',
                'name' => '白俄罗斯',
                'name_ru' => 'Беларусь',
                'mergename' => 'Беларусь',
                'mergename_ru' => 'Беларусь',
                'country' => 3
            ],
        ];
        return $data;
    }
}